package cn.bingoogolapple.bgabanner.demo.ui;

import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.BGALocalImageSize;
import cn.bingoogolapple.bgabanner.demo.App;
import cn.bingoogolapple.bgabanner.demo.ResourceTable;
import cn.bingoogolapple.bgabanner.demo.engine.Engine;
import cn.bingoogolapple.bgabanner.demo.model.BannerModel;
import cn.bingoogolapple.bgabanner.transformer.AlphaPageTransformer;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.Image;
import ohos.agp.components.PageSlider;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GuideAbilitySlice extends AbilitySlice {
    private BGABanner mBackgroundBanner;
    private BGABanner mForegroundBanner;
    private Engine mEngine;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        mEngine = App.getInstance().getEngine();
        getWindow().setWindowFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN, WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        initView();
        setListener();
        processLogic();
    }

    private void initView() {
        super.setUIContent(ResourceTable.Layout_ability_guide);
        mBackgroundBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_guide_background);
        mForegroundBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_guide_foreground);
        mBackgroundBanner.setIconYesElement(0);
        mBackgroundBanner.setIconNoElement(0);
        mForegroundBanner.setIconYesElement(ResourceTable.Media_diamond_select_yes);
        mForegroundBanner.setIconNoElement(ResourceTable.Media_diamond_select_no);
        mForegroundBanner.setOnPageChangeListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
                mBackgroundBanner.getViewPager().onPageSliding(i, v, i1);
                mBackgroundBanner.setCurrentItem(mForegroundBanner.getCurrentItem());
            }

            @Override
            public void onPageSlideStateChanged(int i) {
            }

            @Override
            public void onPageChosen(int i) {
            }
        });

        Button btn_guide_enter = (Button) findComponentById(ResourceTable.Id_btn_guide_enter);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setCornerRadius(15);
        shapeElement.setRgbColor(new RgbColor(RgbColor.fromArgbInt(Color.getIntColor("#6BBEB8"))));
        btn_guide_enter.setBackground(shapeElement);
    }

    private void setListener() {
        /**
         * 设置进入按钮和跳过按钮控件资源 id 及其点击事件
         * 如果进入按钮和跳过按钮有一个不存在的话就传 0
         * 在 BGABanner 里已经帮开发者处理了防止重复点击事件
         * 在 BGABanner 里已经帮开发者处理了「跳过按钮」和「进入按钮」的显示与隐藏
         */
        mBackgroundBanner.setEnterSkipViewIdAndDelegate(getAbility(), ResourceTable.Id_btn_guide_enter, ResourceTable.Id_tv_guide_skip, () -> {
            Intent resultData = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("cn.bingoogolapple.bgabanner.demo")
                    .withAbilityName("cn.bingoogolapple.bgabanner.demo.ui.MainAbility")
                    .build();
            resultData.setOperation(operation);
            startAbility(resultData);

            Intent forwardData = new Intent();
            Operation forwardOperation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("cn.bingoogolapple.bgabanner.demo.ui")
                    .withAbilityName("cn.bingoogolapple.bgabanner.demo.ui.GuideAbility")
                    .build();
            forwardData.setOperation(forwardOperation);
            GuideAbilitySlice.this.stopAbility(forwardData);
        });
    }

    private void processLogic() {
        // Bitmap 的宽高在 maxWidth maxHeight 和 minWidth minHeight 之间
        /**
         * 引导页加载本地数据的方式
         */
        BGALocalImageSize localImageSize = new BGALocalImageSize(720, 1280, 320, 640);
        mBackgroundBanner.setData(null, Image.ScaleMode.CLIP_CENTER,
                ResourceTable.Media_uoko_guide_background_1,
                ResourceTable.Media_uoko_guide_background_2,
                ResourceTable.Media_uoko_guide_background_3);

        mForegroundBanner.setPageTransformer(new AlphaPageTransformer());
        mForegroundBanner.setData(
                localImageSize, Image.ScaleMode.CLIP_CENTER,
                ResourceTable.Media_uoko_guide_foreground_1,
                ResourceTable.Media_uoko_guide_foreground_2,
                ResourceTable.Media_uoko_guide_foreground_3);
    }

    private void loadData(final int count) {
        mEngine.fetchItemsWithItemCount(count).enqueue(new Callback<BannerModel>() {
            @Override
            public void onResponse(Call<BannerModel> call, Response<BannerModel> response) {
            }

            @Override
            public void onFailure(Call<BannerModel> call, Throwable t) {
                new ToastDialog(getContext()).setText("失败").show();
            }
        });
    }
}
